﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BGSpaceShooter;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ParticleSystem.ParticleSystem;

namespace ParticleSystem
{
    public class SteamEffect : IObjectEffect
    {
        private bool isActive;
        public GameObject GameObject { get; set; }

        public ParticleEmitter particleEmmiter;

        private EngineName enginePosition;

        public SteamEffect(GameObject gameObject, EngineName enginePosition)
        {
            GameObject = gameObject;
            this.enginePosition = enginePosition;
            CreateParticleEmitter();
        }

        private void CreateParticleEmitter()
        {
            particleEmmiter = new ParticleEmitter();

            IsActive = false;

            particleEmmiter.MaxParticles = 500;

            particleEmmiter.Duration = TimeSpan.FromSeconds(0.4);
            particleEmmiter.BlendState = BlendState.Additive;
            particleEmmiter.MinHorizontalVelocity = 0;
            particleEmmiter.MaxHorizontalVelocity = 3;

            particleEmmiter.MinVerticalVelocity = -5;
            particleEmmiter.MaxVerticalVelocity = 5;

            particleEmmiter.Gravity = new Vector3(0, 0, 0);

            //    particleEmmiter.EndVelocity = 0.75f;

            particleEmmiter.MinRotateSpeed = -10;
            particleEmmiter.MaxRotateSpeed = 10;

            particleEmmiter.MinStartSize = 0.7f;
            particleEmmiter.MaxStartSize = 0.7f;

            particleEmmiter.MinEndSize = 1.0f;
            particleEmmiter.MaxEndSize = 1.0f;
            particleEmmiter.MinColor = new Color(240, 240, 240, 100);
            particleEmmiter.MaxColor = new Color(255, 255, 255, 100);

            particleEmmiter.Reset();

            particleEmmiter.LoadContent(Display.GraphicsDevice);
            particleEmmiter.LoadParticleEffect(GameContentManager.Content.GetTexture("whitesmoke"));
        }

        #region IObjectEffect Members

        public bool IsActive
        {
            get { return isActive; }
            set
            {
                isActive = value;
                //  particleEmmiter.Reset();
            }
        }

        public bool ShouldVibrate()
        {
            return false;
        }

        public EffectDrawer GetDrawer()
        {
            if (GameObject == null)
            {
                throw new NullReferenceException("No GameObject assigned");
            }
            return new SteamEffectDrawer(this, GameObject);
        }

        public void Update(GameTime gameTime)
        {
            if (GameObject == null)
            {
                throw new NullReferenceException("No GameObject assigned");
            }


            for (int i = 0; i < 7; i++)
                if (this.IsActive)
                {
                    string meshKey = "";
                    switch (enginePosition)
                    {
                        case EngineName.leftZ:
                            meshKey = "WingLeft0";
                            break;
                        case EngineName.rightZ:
                            meshKey = "WingRight0";
                            break;
                        case EngineName.bottomX:
                            meshKey = "Bottom";
                            break;
                        case EngineName.topX:
                            meshKey = "Bow";
                            break;
                    }
                    Vector3 wingPosition = Vector3.Transform(GameObject.Model.Meshes[meshKey].BoundingSphere.Center,
                                               GameObject.ModelMatrix);

                    Vector3 speed = Vector3.Transform(new Vector3(0, 1, 0), GameObject.RotationMatrix);

                    var offset = Vector3.Transform(new Vector3(0, 0.4f, 0), GameObject.RotationMatrix);

                    if (enginePosition == EngineName.bottomX)
                    {
                        offset = Vector3.Transform(new Vector3(0, 0.4f, 0), GameObject.RotationMatrix);
                    }
                    else if (enginePosition == EngineName.topX)
                    {
                        offset = Vector3.Transform(new Vector3(0, -0.5f, 0), GameObject.RotationMatrix);
                        speed = Vector3.Transform(new Vector3(0, -1f, 0), GameObject.RotationMatrix);
                    }

                    particleEmmiter.AddParticle(wingPosition - offset, speed);

                }
            particleEmmiter.Update(gameTime);
        }

        #endregion
    }
}
